﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
using System.Linq;
public class DNHundredGame : Game{

    EginUser user = EginUser.Instance;
    EginDN dn = EginDN.Instance;
    //玩家
    public List<DNHundredUser> users;

    //root
    public GameObject Root;
    //牌的控制
    private NNCardsControl nnCardsControl;

    //音效
    public List<AudioClip> musicMen;
    public List<AudioClip> musicWomen;

    //音效
    public List<AudioClip> textMen;
    public List<AudioClip> textWomen;
    //庄家信息
    public BankerInfo bankerInfo;
    //控制加载下注开始和下注结束预设
    public int num = 0;
    //下注数
    public int betCoinCount = 100;
    //下注数
    public int betPos;
    //对应位置的总筹码数
    public List<UILabel> poolChips;
    //自己在相应位置下的筹码数
    public List<UILabel> ownChips;
    public MyOwnInfo myOwnInfo;

    public GameObject SelectBtns;
    public GameObject NoSelectBtns;
    //筹码
    public GameObject chip;

    public GameObject textItemPre;
    public GameObject faceItemPre;
    public GameObject grid;
    public CardsControl cardsControl;
    public DNHundredPanel dnHundredPanel;

    public List<int> canBetCoinCount;

    #region  牛牛收到的服务器消息
    //牌型
    private int pokerType { get; set; }
    #endregion



    public string[] dnText = {
        "大家好，很高兴见到各位！",
        "玩就玩大的，抢庄4倍都行！",
        "手里有牛牛金币不用愁",
        "我就呵呵了",
        "有牛了，看我大杀四方!",
        "发牌就抢庄，赢牌心不慌！",
        "不要走决战到天亮"
    };

    void Awake()
    {
        base.StartGameSocket();
        SocketManager.Instance.socketListener = this;      
    }
  
    void Start()
    {       
        betCoinCount = 100;
        //加载玩家
        LoadUser();
       
        if (dn.isReconnect == true)
        {
            dn.isReconnect = false;
            NN100Reconnect();
        }      
    }

    /// <summary>
    /// 监听服务器消息
    /// </summary>
    /// <param name="data"></param>
    public override void SocketReceiveMessage(byte[] data)
    {
        base.SocketReceiveMessage(data);
        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(4).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        int TempMsgID = ProtoDataToMsg.Byte2ushort(dataId);

        #region 牛牛玩家加入离开房间返回
        if (TempMsgID == MsgId.RoomUserInfoBroadcast)
        {
            Pb.RoomUserInfoBroadcast newpb = Pb.RoomUserInfoBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.UserInfo.SeatStatus == SEAT_STATUS.Sit)
            {
                Debug.Log("有玩家加入了" + Time.time);
                //加入房间
                user.gameUserInfoList.Add(newpb.UserInfo);               
                //判断新加入的玩家是不是庄家
                Debug.Log("新加入的玩家是否是庄家" + newpb.UserInfo.IsBanker);
                
                if(newpb.UserInfo.IsBanker == true){
                    if(newpb.UserInfo.AvatarUrl != ""){
                        WXAvatarManager._instance.GetWxAvatar(newpb.UserInfo.Uid,bankerInfo.wxAvatar,newpb.UserInfo.AvatarUrl);
                        bankerInfo.wxAvatar.gameObject.SetActive(true);
                        //bankerInfo.avatar.gameObject.SetActive(true);
                        bankerInfo.gameObject.SetActive(true);
                    }
                    else
                    {
                        bankerInfo.avatar.spriteName = newpb.UserInfo.AvatarId.ToString();
                        bankerInfo.avatarBtn.GetComponent<UIButton>().normalSprite = newpb.UserInfo.AvatarId.ToString();
                        bankerInfo.nameLabel.text = newpb.UserInfo.Nickname;
                        bankerInfo.goldLabel.text = user.ReturnMoney(newpb.UserInfo.BagCoinCount);
                        bankerInfo.uid = newpb.UserInfo.Uid;
                        bankerInfo.gameObject.SetActive(true);
                        StartCoroutine(GetWWWFACE(bankerInfo.wxAvatar, newpb.UserInfo.AvatarUrl));
                    }
                    
                }                   
                
                              
            }
            else if (newpb.UserInfo.SeatStatus == SEAT_STATUS.Leave)
            {
                if (dn.roomStatus != ROOM_STATUS.Gaming)
                {
                    //离开房间                    
                    for (int i = 0; i < user.gameUserInfoList.Count; i++)
                    {
                        if (newpb.UserInfo.Uid == user.gameUserInfoList[i].Uid)
                        {
                            user.gameUserInfoList.RemoveAt(i);
                            //如果离开的是庄家
                            if(newpb.UserInfo.IsBanker == true){
                                bankerInfo.wxAvatar.gameObject.SetActive(false);
                                bankerInfo.gameObject.SetActive(false);
                            }                          
                            break;
                        }
                    }
                    //如果离开的是座位上的人
                    for (int i = 0; i < dn.chairUserList.Count;i++ )
                    {
                        if(newpb.UserInfo.Uid == dn.chairUserList[i].Uid){
                            users[i].wxAvatar.gameObject.SetActive(false);
                            users[i].avatar.gameObject.SetActive(false);
                            users[i].goldLabel.text = "上座";
                        }
                    }
                }
            }
        }
        #endregion

        #region 牛牛Game消息
      
        //游戏开始
        else if (TempMsgID == MsgId.Nn100RoomBeginNotification)
        {
            Pb.Nn100RoomBeginNotification newpb = Pb.Nn100RoomBeginNotification.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            dn.bankerInfo = newpb.BankerUserInfo;
            dn.roomStatus = ROOM_STATUS.Gaming;
            dn.chairUserList = newpb.ChairUserList.ToList();
            

        }
        //开始下注倒计时广播
        else if (TempMsgID == MsgId.Nn100BeginBroadcast)
        {
            Pb.Nn100BeginBroadcast newpb = Pb.Nn100BeginBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            StartCoroutine(OneGameStart(newpb));
            
        }
        //下注
        else if (TempMsgID == MsgId.Nn100BetResponse)
        {
            Pb.Nn100BetResponse newpb = Pb.Nn100BetResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if(newpb.Status == STATUS.Success){
                for (int i = 0; i < newpb.ChipInfo.PoolChipList.Count; i++)
                {
                    poolChips[i].text = user.ReturnMoney(newpb.ChipInfo.PoolChipList[i]);
                    ownChips[i].text = user.ReturnMoney(newpb.ChipInfo.OwnChipList[i]);
                    if (newpb.ChipInfo.OwnChipList[i] > 0)
                    {
                        ownChips[i].transform.parent.gameObject.SetActive(true);
                    }
                }
                //显示可下注的图标
                Debug.Log("newpb.AvailableBetList.Count = " + newpb.AvailableBetList.Count + "myOwnInfo.betBtns.Count = " + myOwnInfo.betBtns.Count);
                for (int i = newpb.AvailableBetList.Count; i < myOwnInfo.betBtns.Count; i++)
                {
                    myOwnInfo.NoSelectOfActives[i].SetActive(false);
                    myOwnInfo.betBtns[i].SetActive(false);
                }
                for (int i = newpb.AvailableBetList.Count - 1; i >= 0; i--)
                {                   
                    if (newpb.BagCoinCount < canBetCoinCount[i])
                    {
                        Debug.Log(111);
                        Debug.Log(myOwnInfo.betBtns[i].name);
                        Debug.Log(myOwnInfo.NoSelectOfActives[i].name);
                        myOwnInfo.betBtns[i].SetActive(false);
                        myOwnInfo.NoSelectOfActives[i].SetActive(false);
                    }
                }
                         
                //更新自己的金币数
                myOwnInfo.goldLabel.text = user.ReturnMoney(newpb.BagCoinCount);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject obj = NGUITools.AddChild(Root,(GameObject)Resources.Load("LogMsg"));
            }           
        }
        //下注广播
        else if (TempMsgID == MsgId.Nn100BetBroadcast)
        {
            Pb.Nn100BetBroadcast newpb = Pb.Nn100BetBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            for (int i = 0; i < newpb.ChipInfo.PoolChipList.Count; i++)
            {
                poolChips[i].text = user.ReturnMoney(newpb.ChipInfo.PoolChipList[i]);                             
            }
            //克隆筹码
            CloneChips(newpb.BetPosition,newpb.BetCoinCount);
        }
        else if (TempMsgID == MsgId.Nn100ChairBroadcast)
        {
            Pb.Nn100ChairBroadcast newpb = Pb.Nn100ChairBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            dn.chairUserList = newpb.ChairUserList.ToList();
            for (int i = 0; i < newpb.ChairUserList.Count;i++ )
            {
                users[i].avatar.spriteName = newpb.ChairUserList[i].AvatarId.ToString();
                users[i].avatar.gameObject.SetActive(true);
                users[i].goldLabel.text = user.ReturnMoney(newpb.ChairUserList[i].BagCoinCount);
                users[i].uid = newpb.ChairUserList[i].Uid;
            }
        }        
        //游戏结算
        else if (TempMsgID == MsgId.Nn100GameOverBroadcast)
        {
            Pb.Nn100GameOverBroadcast newpb = Pb.Nn100GameOverBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            dn.roomStatus = ROOM_STATUS.GameOver;
            dn.bankerInfo = newpb.BankerUserInfo;
            dn.chairUserList = newpb.ChairUserList.ToList();

            //可下注按钮隐藏
            //SelectBtns.SetActive(false);

            //不可申请上下庄
            dnHundredPanel.bankerUpBtn.GetComponent<BoxCollider>().enabled = false;
            dnHundredPanel.bankerDownBtn.GetComponent<BoxCollider>().enabled = false;

            //更新房间中庄家  椅子上的玩家的金币数
            for (int i = 0; i < user.gameUserInfoList.Count;i++ )
            {
                if(user.gameUserInfoList[i].Uid == newpb.BankerUserInfo.Uid){
                    user.gameUserInfoList[i].BagCoinCount = newpb.BankerUserInfo.BagCoinCount;
                }
                for (int j = 0; j < newpb.ChairUserList.Count;j++ )
                {
                    if(user.gameUserInfoList[i].Uid == newpb.ChairUserList[j].Uid){
                        user.gameUserInfoList[i].BagCoinCount = newpb.ChairUserList[j].BagCoinCount;
                    }
                }
            }

            StartCoroutine(AfterBet(newpb));            
        }
        //上庄
        else if (TempMsgID == MsgId.Nn100BankerResponse)
        {
            Pb.Nn100BankerResponse newpb = Pb.Nn100BankerResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if(newpb.Status == STATUS.Success){
                //dnHundredPanel.bankerUpBtn.SetActive(false);
                //dnHundredPanel.bankerDownBtn.SetActive(true);
            }
            else
            {
                
            }
            user.logMsg = newpb.Msg;
            GameObject msg = NGUITools.AddChild(Root, (GameObject)Resources.Load("LogMsg"));
        }
        //下庄
        else if (TempMsgID == MsgId.Nn100CancelBankerResponse)
        {
            Pb.Nn100CancelBankerResponse newpb = Pb.Nn100CancelBankerResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                //dnHundredPanel.bankerUpBtn.SetActive(true);
                //dnHundredPanel.bankerDownBtn.SetActive(false);
                dnHundredPanel.isCanBet = true;
            }
            else
            {
               
            }

            user.logMsg = newpb.Msg;
            GameObject msg = NGUITools.AddChild(Root, (GameObject)Resources.Load("LogMsg"));
        }
        #endregion 百人牛牛游戏中
        //离开房间
        else if (TempMsgID == MsgId.RoomLeaveResponse)
        {
            Pb.RoomLeaveResponse newpb = Pb.RoomLeaveResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                //EginUser.Instance.gameUserInfoList.Clear();
                user.gameUserInfoList.Clear();
                Application.LoadLevel(ScenePath.Hall);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        else if (TempMsgID == MsgId.RoomDismissBroadcast)
        {
            Pb.RoomDismissBroadcast newpb = Pb.RoomDismissBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            EginUser.Instance.gameUserInfoList.Clear();
            Application.LoadLevel(ScenePath.Hall);
        }
        //走势图
        else if (TempMsgID == MsgId.Nn100HistoryResponse)
        {
            Pb.Nn100HistoryResponse newpb = Pb.Nn100HistoryResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            GameObject trendPanel = NGUITools.AddChild(Root,(GameObject)Resources.Load(GameDN100PrefabPath.Trend));
            TrendInfo trendInfo = trendPanel.GetComponent<TrendInfo>();
            trendInfo.resultList1 = newpb.ScoreList1.ToList();
            trendInfo.resultList2 = newpb.ScoreList2.ToList();
            trendInfo.resultList3 = newpb.ScoreList3.ToList();
            trendInfo.resultList4 = newpb.ScoreList4.ToList(); 
        }
        //站立用户列表
        else if (TempMsgID == MsgId.Nn100StandListResponse)
        {
            Pb.Nn100StandListResponse newpb = Pb.Nn100StandListResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            GameObject playerPanel = NGUITools.AddChild(Root,(GameObject)Resources.Load(GameDN100PrefabPath.Players));
            PlayerInfo playerInfo = playerPanel.GetComponent<PlayerInfo>();
            playerInfo.userList = newpb.StandUserList.ToList();

            //将站立的用户添加进百人牛牛玩家列表
            for (int i = 0; i < newpb.StandUserList.Count;i++ )
            {
                if(!user.gameUserInfoList.Contains(newpb.StandUserList[i])){
                    user.gameUserInfoList.Add(newpb.StandUserList[i]);
                }
            }

            //更新房间中站立玩家的金币数
            for (int i = 0; i < user.gameUserInfoList.Count;i++ )
            {
                for (int j = 0; j < newpb.StandUserList.Count;j++ )
                {
                    if(user.gameUserInfoList[i].Uid == newpb.StandUserList[j].Uid){
                        user.gameUserInfoList[i].BagCoinCount = newpb.StandUserList[j].BagCoinCount;
                    }
                }
            }
        }

        #region 文字表情聊天
        else if (TempMsgID == MsgId.RoomChatBroadcast)
        {
            Pb.RoomChatBroadcast newpb = Pb.RoomChatBroadcast.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            StartCoroutine(TextFaceChat(newpb));
        }
        #endregion
        //网络异常
        else if (TempMsgID == MsgId.NetWorkAbnormalMessage)
        {
            GameObject NetWorkAbnormal = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("NetWorkAbnormal")));
        }
    }
    /// <summary>
    /// 加载玩家状态 +添加第三人进入房间旁观
    /// </summary>
    void LoadUser()
    {
       
        Debug.Log("加载玩家");
        for (int i = 0; i < user.gameUserInfoList.Count;i++ )
        {
            //显示自己的信息
            if(user.gameUserInfoList[i].Uid == int.Parse(user.Uid)){
                myOwnInfo.nameLabel.text = user.gameUserInfoList[i].Nickname;
                myOwnInfo.goldLabel.text = user.ReturnMoney(user.gameUserInfoList[i].BagCoinCount);
            }          
        }       
        //显示庄家的信息

        if (dn.bankerInfo.AvatarId != 0 && dn.bankerInfo.BagCoinCount != 0)
        {
            if (dn.bankerInfo.AvatarUrl != "")
            {
                WXAvatarManager._instance.GetWxAvatar(dn.bankerInfo.Uid,bankerInfo.wxAvatar,dn.bankerInfo.AvatarUrl);
                bankerInfo.wxAvatar.gameObject.SetActive(true);
                //bankerInfo.avatar.gameObject.SetActive(false);                
            }
            else
            {
                bankerInfo.avatar.spriteName = dn.bankerInfo.AvatarId.ToString();
                bankerInfo.avatarBtn.GetComponent<UIButton>().normalSprite = dn.bankerInfo.AvatarId.ToString();
                bankerInfo.nameLabel.text = dn.bankerInfo.Nickname;
                bankerInfo.goldLabel.text = user.ReturnMoney(dn.bankerInfo.BagCoinCount);
                bankerInfo.uid = dn.bankerInfo.Uid;               
                //StartCoroutine(GetWWWFACE(bankerInfo.wxAvatar, dn.bankerInfo.AvatarUrl));     
            }
            bankerInfo.gameObject.SetActive(true);           
        }                                         
                    
        //显示天地玄黄的筹码信息
        for (int i = 0; i < dn.chipInfo.PoolChipList.Count;i++ )
        {
            poolChips[i].text = user.ReturnMoney(dn.chipInfo.PoolChipList[i]);
            ownChips[i].text = user.ReturnMoney(dn.chipInfo.OwnChipList[i]);
            //进入房间时克隆天地玄黄位置上的筹码
            ReconnectCloneChips(dn.chipInfo.OwnChipList[i],i + 1);
            if (dn.chipInfo.OwnChipList[i] > 0)
            {
                ownChips[i].transform.parent.gameObject.SetActive(true);
            }
        }
        //显示上座的玩家信息
        Debug.Log(dn.chairUserList.Count);
        for (int i = 0; i < dn.chairUserList.Count;i++ )
        {
            if(dn.chairUserList[i].AvatarUrl != ""){
                WXAvatarManager._instance.GetWxAvatar(dn.chairUserList[i].Uid,users[i].wxAvatar,dn.chairUserList[i].AvatarUrl);
                users[i].wxAvatar.gameObject.SetActive(true);
                users[i].avatar.gameObject.SetActive(true);
            }
            else
            {
                users[i].avatar.spriteName = dn.chairUserList[i].AvatarId.ToString();
                users[i].avatarBtn.GetComponent<UIButton>().normalSprite = dn.chairUserList[i].AvatarId.ToString();
                users[i].avatar.gameObject.SetActive(true);
                users[i].goldLabel.text = user.ReturnMoney(dn.chairUserList[i].BagCoinCount);
                users[i].uid = dn.chairUserList[i].Uid;
                StartCoroutine(GetWWWFACE(users[i].wxAvatar, dn.chairUserList[i].AvatarUrl));
                Debug.Log("LoaderUser() 座位上的玩家Uid = " + dn.chairUserList[i].Uid);
            }
            
        }
    }

    /// <summary>
    /// 游戏开始 可以下注
    /// </summary>
    /// <param name="newpb"></param>
    /// <returns></returns>
    IEnumerator OneGameStart(Nn100BeginBroadcast newpb)
    {
        Debug.Log("游戏开始");
       
        GameObject betStart = NGUITools.AddChild(Root,(GameObject)Resources.Load(GameDN100PrefabPath.BetStart));
        yield return new WaitForSeconds(1f);
        dnHundredPanel.isCanBet = true;
        Destroy(betStart);
        //加载倒计时时钟
        GameObject clock = NGUITools.AddChild(Root, (GameObject)Resources.Load(GameDN100PrefabPath.Clock));
        CutDownTime2 cutDownTime2 = clock.GetComponent<CutDownTime2>();
        cutDownTime2.passTime = newpb.Seconds;
        //筹码可选按钮显示
        if(dn.isBanker == false){
            Debug.Log("游戏开始，我不是庄家，显示可下注按钮");
            SelectBtns.SetActive(true);
            //显示可下注按钮
            for (int i = 0; i < myOwnInfo.betBtns.Count;i++ )
            {
                if(int.Parse(user.coinCount) >= canBetCoinCount[i]){
                    myOwnInfo.betBtns[i].SetActive(true);
                    //myOwnInfo.NoSelectOfActives[i].SetActive(false);                  
                }
                if (betCoinCount == canBetCoinCount[i])
                {
                    myOwnInfo.NoSelectOfActives[i].SetActive(true);
                }
            }
        }
        else
        {
            SelectBtns.SetActive(false);
        }               
    }

    IEnumerator AfterBet(Nn100GameOverBroadcast newpb)
    {
        //加载买定离手
        Debug.Log("买定离手" + Time.time);
        GameObject betEnd = NGUITools.AddChild(GameObject.Find("UI Root"),(GameObject)Resources.Load(GameDN100PrefabPath.BetEnd));
        yield return new WaitForSeconds(1f);
        Destroy(betEnd);
        StartCoroutine(GameOver(newpb));
    }

    IEnumerator GameOver(Nn100GameOverBroadcast newpb)
    {      
        //加载cardsPanel
        Debug.Log("加载牌" + Time.time);
        //展示庄家和闲家的牌 
        cardsControl.StartShowCards(newpb.PokerItem0.CardList.ToList(), newpb.PokerItem1.CardList.ToList(), newpb.PokerItem2.CardList.ToList(), newpb.PokerItem3.CardList.ToList(), newpb.PokerItem4.CardList.ToList());
        //先初始化一下
        //cardsControl.Init();
        GameObject cards = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)Resources.Load(GameDN100PrefabPath.Cards));        
        yield return new WaitForSeconds(2f);
        Debug.Log("设置牌" + Time.time);
                      
        //展示对应位置的牌型
        List<int> pokerTypeList = new List<int>();
        pokerTypeList.Add(newpb.PokerItem0.PokerType);
        pokerTypeList.Add(newpb.PokerItem1.PokerType);
        pokerTypeList.Add(newpb.PokerItem2.PokerType);
        pokerTypeList.Add(newpb.PokerItem3.PokerType);
        pokerTypeList.Add(newpb.PokerItem4.PokerType);
        //cardsControl.pokerTypeList = pokerTypeList;
        cardsControl.ShowPokerType(pokerTypeList);

        //加载游戏结算界面
        Debug.Log("加载游戏结算界面");
        
        GameObject gameOver = NGUITools.AddChild(Root, (GameObject)Resources.Load(GameDN100PrefabPath.GameOver));
        DN100GameOver dn100GameOver = gameOver.GetComponent<DN100GameOver>();
        dn100GameOver.newpb = newpb;
        dn100GameOver.bankerScore = newpb.BankerScore;
        dn100GameOver.ownScore = newpb.UserScore;
        dn100GameOver.ownCoinCount = newpb.UserBagCoinCount;
               
        //更新自己的金币数
        user.coinCount = newpb.UserBagCoinCount.ToString();
        myOwnInfo.goldLabel.text = user.ReturnMoney(newpb.UserBagCoinCount);

        //显示天地玄黄位置的输赢情况
        pokerTypeList.RemoveAt(0);
        dn100GameOver.ShowResult(newpb.PokerItem0.PokerType, pokerTypeList);
    }

    /// <summary>
    /// 文字表情聊天   
    /// </summary>
    /// <param name="newpb"></param>
    /// <returns></returns>
    IEnumerator TextFaceChat(RoomChatBroadcast newpb)
    {
        int tempIndex = 0;
        for (int i = 0; i < user.gameUserInfoList.Count; i++)
        {
            if (newpb.FromUid == user.gameUserInfoList[i].Uid)
            {
                int tempSeatid = user.gameUserInfoList[i].SeatId;
                tempIndex = dn.returnOtherSeatIndex(tempSeatid);
                if (newpb.MsgType == RoomChatType.TextId)
                {
                    GameObject textChat = Instantiate(textItemPre);
                    textChat.transform.parent = grid.transform;
                    textChat.transform.localScale = Vector3.one;
                    textChat.transform.localPosition = Vector3.zero;
                    textChat.transform.Find("Label").GetComponent<UILabel>().text = dnText[int.Parse(newpb.Content) - 1];
                    if(user.gameUserInfoList[i].AvatarUrl != ""){
                        UITexture uiTexture = textChat.transform.Find("WXAvatar").GetComponent<UITexture>();
                        WXAvatarManager._instance.GetWxAvatar(user.gameUserInfoList[i].Uid,uiTexture,user.gameUserInfoList[i].AvatarUrl);
                        uiTexture.gameObject.SetActive(true);
                    }
                    else
                    {
                        textChat.transform.Find("Avatar").GetComponent<UISprite>().spriteName = user.gameUserInfoList[i].AvatarId.ToString();
                    }                   
                    StartCoroutine(DestroyChat(textChat));                  
                    if (user.gameUserInfoList[i].Sex == SEX.Male)
                    {
                        //SoundModel.PlaySound(textMen[int.Parse(newpb.Content) - 1]);
                    }
                    else
                    {
                        //SoundModel.PlaySound(textWomen[int.Parse(newpb.Content) - 1]);
                    }
                }
                else if (newpb.MsgType == RoomChatType.MovieClip)
                {
                    GameObject faceChat = Instantiate(faceItemPre);
                    faceChat.transform.parent = grid.transform;
                    faceChat.transform.localScale = Vector3.one;
                    faceChat.transform.localPosition = Vector3.zero;
                    faceChat.transform.Find("Face").GetComponent<UISprite>().spriteName = newpb.Content;
                    if (user.gameUserInfoList[i].AvatarUrl != "")
                    {
                        UITexture uiTexture = faceChat.transform.Find("WXAvatar").GetComponent<UITexture>();
                        WXAvatarManager._instance.GetWxAvatar(user.gameUserInfoList[i].Uid, uiTexture, user.gameUserInfoList[i].AvatarUrl);
                        uiTexture.gameObject.SetActive(true);
                    }
                    else
                    {
                        faceChat.transform.Find("Avatar").GetComponent<UISprite>().spriteName = user.gameUserInfoList[i].AvatarId.ToString();
                    } 
                    
                    StartCoroutine(DestroyChat(faceChat));
                }
                grid.GetComponent<UIGrid>().enabled = true;
            }
        }
        yield return new WaitForSeconds(2.5f);
        //Users[tempIndex].textObj.SetActive(false);
        //Users[tempIndex].face.gameObject.SetActive(false);
    }

    IEnumerator DestroyChat(GameObject obj)
    {
        yield return new WaitForSeconds(2f);
        Destroy(obj);
        grid.GetComponent<UIGrid>().enabled = true;
    }


    /// <summary>
    /// 初始化玩家状态
    /// </summary>
    public void UserInit()
    {
        for (int i = 0; i < users.Count; i++)
        {
            //Users[i].Init();
        }
    }

   

    #region 重连
    /// <summary>
    /// 斗牛百人场重连
    /// </summary>
    void NN100Reconnect()
    {

        Nn100ReconnectResponse newpb = dn.nn100ReconnectResponse;       
        if(newpb.Status == STATUS.Success){

            //显示自己的信息
            myOwnInfo.nameLabel.text = user.nickname;
            myOwnInfo.goldLabel.text = user.ReturnMoney(int.Parse(user.coinCount));

            //显示庄家信息
            dn.bankerInfo = newpb.BankerUserInfo;                      
            
            bankerInfo.avatar.spriteName = newpb.BankerUserInfo.AvatarId.ToString();
            bankerInfo.avatarBtn.GetComponent<UIButton>().normalSprite = newpb.BankerUserInfo.AvatarId.ToString();
            bankerInfo.nameLabel.text = newpb.BankerUserInfo.Nickname;
            bankerInfo.goldLabel.text = user.ReturnMoney(newpb.BankerUserInfo.BagCoinCount);
            bankerInfo.uid = newpb.BankerUserInfo.Uid;
            Debug.Log("重连之后的庄家uid = " + bankerInfo.uid);
            bankerInfo.gameObject.SetActive(true);
            StartCoroutine(GetWWWFACE(bankerInfo.wxAvatar, newpb.BankerUserInfo.AvatarUrl));
            
            //将庄家加进百人牛牛玩家列表中
            //user.gameUserInfoList.Add(newpb.BankerUserInfo);
                        
            //显示椅子上的玩家
            dn.chairUserList = newpb.ChairUserList.ToList();
            for (int i = 0; i < newpb.ChairUserList.Count;i++ )
            {
                //将椅子上的玩家加进百人牛牛玩家列表中
                //user.gameUserInfoList.Add(newpb.ChairUserList[i]);
                users[i].avatar.spriteName = newpb.ChairUserList[i].AvatarId.ToString();
                users[i].avatarBtn.GetComponent<UIButton>().normalSprite = newpb.ChairUserList[i].AvatarId.ToString();
                users[i].avatar.gameObject.SetActive(true);
                users[i].goldLabel.text = user.ReturnMoney(newpb.ChairUserList[i].BagCoinCount);
                users[i].uid = newpb.ChairUserList[i].Uid;
                Debug.Log("重连之后的椅子上玩家uid = " + users[i].uid);
            }
            //显示天地玄黄四个位置上的筹码信息
            //各个位置上的总筹码数
            for (int i = 0; i < newpb.ChipInfo.PoolChipList.Count;i++ )
            {
                poolChips[i].text = user.ReturnMoney(newpb.ChipInfo.PoolChipList[i]);
                //克隆筹码
                //ReconnectCloneChips(newpb.ChipInfo.PoolChipList[i],i + 1);

            }
            //自己在对应位置上所下的筹码数
            for (int i = 0; i < newpb.ChipInfo.OwnChipList.Count;i++ )
            {
                ownChips[i].text = user.ReturnMoney(newpb.ChipInfo.OwnChipList[i]);
                if(newpb.ChipInfo.OwnChipList[i] > 0){
                    ownChips[i].transform.parent.gameObject.SetActive(true);
                }
            }
        }
        else
        {
            user.logMsg = newpb.Msg;
            GameObject msg = NGUITools.AddChild(Root,(GameObject)Resources.Load("LogMsg"));
        }
    }
    #endregion 重连
 


    /// <summary>
    /// 克隆筹码
    /// </summary>
    void CloneChips(int betPosition,int betCoinCount)
    {
        Vector3 tempDestination = Vector3.zero;
        int cloneNum = 0;
        
        //确定克隆数量
        if(betCoinCount == 100){
            cloneNum = 1;
        }
        else if (betCoinCount == 1000)
        {
            cloneNum = 2;
        }
        else if (betCoinCount == 10000)
        {
            cloneNum = 3;
        }
        else if (betCoinCount == 100000)
        {
            cloneNum = 4;
        }
        else if (betCoinCount == 1000000)
        {
            cloneNum = 5;
        }       
        //克隆
        for (int i = 0; i < cloneNum;i++ )
        {
            //确定位置
            if (betPosition == 1)
            {
                tempDestination = new Vector3(Random.Range(-455, -280), Random.Range(10, 110), 0);
            }
            else if (betPosition == 2)
            {
                tempDestination = new Vector3(Random.Range(-200, -30), Random.Range(10, 110), 0);
            }
            else if (betPosition == 3)
            {
                tempDestination = new Vector3(Random.Range(50, 220), Random.Range(10, 110), 0);
            }
            else if (betPosition == 4)
            {
                tempDestination = new Vector3(Random.Range(300, 470), Random.Range(10, 110), 0);
            }
            Debug.Log("tempDestination = " + tempDestination);
            //chip.GetComponent<ChipsMove>().setDestination(tempDestination);
            chip.GetComponent<ChipsMove>().destination = tempDestination;

            GameObject chipClone = Instantiate(chip);
            chipClone.transform.SetParent(dnHundredPanel.transform);
            chipClone.transform.localPosition = chip.transform.localPosition;
            chipClone.transform.localScale = chip.transform.localScale;
            chipClone.SetActive(true);            
        }
    }


    /// <summary>
    /// 重连克隆筹码
    /// </summary>
    void ReconnectCloneChips(int count,int pos)
    {
        Vector3 tempPos = Vector3.zero;
       
        while(count > 100){
            int num = 0;
            int n = 0;
            if (count >= 1000000)
            {
                num = count / 1000000;
                count %= 1000000;
                n = 5;
            }
            else if(count >= 100000){
                num = count / 100000;
                count %= 100000;
                n = 4;
            }
            else if (count >= 10000)
            {
                num = count / 10000;
                count %= 10000;
                n = 3;
            }
            else if (count >= 1000)
            {
                num = count / 1000;
                count %= 1000;
                n = 2;
            }
            else if (count >= 100)
            {
                num = count / 100;
                count %= 100;
                n = 1;
            }

            for (int i = 0; i < num * n; i++)
            {
                if (pos == 1)
                {
                    tempPos = new Vector3(Random.Range(-455, -280), Random.Range(10, 110), 0);
                }
                else if (pos == 2)
                {
                    tempPos = new Vector3(Random.Range(-200, -30), Random.Range(10, 110), 0);
                }
                else if (pos == 3)
                {
                    tempPos = new Vector3(Random.Range(50, 220), Random.Range(10, 110), 0);
                }
                else if (pos == 4)
                {
                    tempPos = new Vector3(Random.Range(300, 470), Random.Range(10, 110), 0);
                }
                chip.GetComponent<ChipsMove>().destination = tempPos;

                GameObject chipClone = Instantiate(chip);
                chipClone.transform.SetParent(dnHundredPanel.transform);
                chipClone.transform.localPosition = tempPos;
                chipClone.transform.localScale = chip.transform.localScale;
                chipClone.SetActive(true);
            }
        }
        
    }


    public void InitChips()
    {
        for (int i = 0; i < poolChips.Count;i++ )
        {
            poolChips[i].text = "0";
            ownChips[i].transform.parent.gameObject.SetActive(false);
        }
        GameObject[] chips = GameObject.FindGameObjectsWithTag("Chip");
        for (int i = 0; i < chips.Length;i++ )
        {
            if(chips[i].activeSelf == true){
                Destroy(chips[i]);
            }           
        }
    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    IEnumerator GetWWWFACE(UITexture texture, string url)
    {

        if (url != "")
        {
            WWW www = new WWW(url);


            yield return www;
            if (www.isDone)
            {
                if (www.error == null) { }
                texture.mainTexture = www.texture;
                texture.gameObject.SetActive(true);
            }
            else
            {

            }
            www.Dispose();
        }

    }
}
